#!/usr/bin/make -f

default: help all
	date -u

project?=tizen
package?=${project}-helper

SHELL=/bin/bash -o pipefail
export SHELL

self?=$(lastword $(MAKEFILE_LIST))
thisdir:=$(shell dirname $(realpath ${self}))
make=${MAKE} -f ${self}
export make

home?=${HOME}
export home

user?=$(shell echo "${USER}" || echo nobody)
tmpdir?=${CURDIR}/tmp
tmpdir?=${home}/tmp/tmp-${USER}.tmp/${package}/${CURDIR}/tmp
osc_url?=https://api.${project}.org
gbs_conf?=${thisdir}/config/gbs.conf

profile?=${project}
export profile

profile_name=$(shell echo ${profile} | cut -d. -f2-)

package?=$(shell grep '^Name *:' packaging/*.spec \
 | sed -e 's|.*:[ \t]*\(.*\)|\1|g' \
 || echo TODO)
version?=$(shell grep '^Version *:' packaging/*.spec \
 | sed -e 's|.*:[ \t]*\([0-9].*\)|\1|g' \
 || echo 0)
revision?=$(shell grep '^Release *:' packaging/*.spec \
 | sed -e 's|.*:[ \t]*\([0-9].*\)|\1|g' \
 || echo 0)
date?=$(shell date -u +%s)
stamp?=$(shell date -u +%Y%m%d.%H%M%S)
export stamp


local_revision?=${stamp}${user}+${profile_name}
host_arch?=$(shell uname -m || echo "unknown")
arch?=${host_arch}
export arch

tmp?=._$(shell date -u +%s).tmp
srcs?=${SELF} \
 $(shell find ${thisdir}/config -iname "*.in")

gbs_file?=${thisdir}/config/gbs.conf

profiles?=$(shell grep '\[profile' ${gbs_file} \
 | sed -e 's|\[\(profile\..*\)\]|\1|g' \
 | sort \
)

urls?=$(shell grep 'url=' ${gbs_file} \
 | sed -e 's|url=\(.*\)|\1|g' \
)


sudo?=sudo


gbs_dir?=${home}/tmp/gbs/tmp-GBS-${profile_name}
repo_dir?=${gbs_dir}/local/repos/${profile_name}/${arch}
mount_dir?=${gbs_dir}/local/BUILD-ROOTS/scratch.${arch}.0/proc
src_rpm?=${repo_dir}/SRPMS/${package}-${version}-${revision}.src.rpm
gbs?=gbs
export gbs

help: ${thisdir}/README.txt
	cat $<
	@echo '# Usage:'
	@echo 'make rule/profile/profile.distro_$$version_$$profile_$$arch'
	@echo '# Existing profiles:'
	@echo "${profiles}" | tr ' ' '\n'

print:
	@echo "arch: ${arch}"
	@echo "urls:"
	@echo "${urls}"
	@echo "profiles:"
	${make} print/profiles
	@echo "self: ${self}"
	@echo "thisdir: ${thisdir}"

print/profiles:
	@echo "${profiles}"


clean:
	rm -fv *~ *.tmp ${tmpdir}/*

install: ${gbs_file}
	install -d ${DESTDIR}/usr/share/${package}/
	install $< ${DESTDIR}/usr/share/${package}/

deploy: ${home}/.gbs.conf
	ls -l $^

${home}/.gbs.conf: ${gbs_file}
	mkdir -p ${@D}
	-mv -fv ${home}/.gbs.conf ${home}/.gbs.conf${tmp}
	ln -fs $< ${@}

log/%: ${tmpdir}
	@mkdir -p "${tmpdir}"
	script -e -c "${make} rule/${@F}" ${tmpdir}/${@F}

tee/%:
	@mkdir -p "${tmpdir}"
	time stdbuf -o 0 ${make} rule/${@F} | tee "${tmpdir}/${@F}"

revision: packaging
	-sed -e 's|Release:\( *\)[^ ]*|Release:\1${local_revision}|g' -i packaging/*.spec \
 || echo "ignored: $@"

packaging:
	-cd ../packaging/.. \
	&& grep '^Release' packaging/*.spec \
 || echo "ignored: $@"

revision-clean: packaging
	-sed -e 's|Release:\( *\)[^ ]*|Release:\10|g' -i packaging/*.spec \
 || echo "ignored: $@"

gbs-build: ${home}/.gbs.conf ${srcs}
	HOME=${home} gbs build -P ${profile} --arch ${arch} --include-all

rule/gbs-build-sed-${profile}: ${home}/.gbs.conf ${srcs}
	HOME=${home} gbs build -P ${profile} --arch ${arch} --include-all \
 | sed -e 's|^\[.*s\] ||g' -e 's|^../../../||g' \


#TODO: refactor
gbs_builder?=gbs-build-sed
gbs_builder_rule?=gbs-build-sed-${profile}
logfile?=${gbs_builder_rule}

gbs-build-track/%: print
	@mkdir -p "${tmpdir}"
	{ ${make} tee/${gbs_builder_rule} rpm \
 && ln -fs "${logfile}" "${tmpdir}/${logfile}.log._success.tmp" \
 && rm -f "${tmpdir}/${logfile}.log._fail.tmp" ; \
 } || { ln -fs "${logfile}" "${tmpdir}/${logfile}.log._fail.tmp" ; exit $$? ; }

rule/gbs-default: gbs-build-track/${profile}

reset/%: umount binfmt
	${sudo} sync

rule/gbs-try:
	@echo "#{ $@: $^"
	${make} rule/gbs-default \
 || ${make} reset/${profile} rule/gbs-default
	@echo "#} $@: $^"

gbs-try/%:

gbs/%: tee/gbs-try
	date

#${src_rpm}:
#	echo "TODO"

rpm: ${src_rpm} print
	@ls ${repo_dir}/logs/success/${package}-${version}-${revision}/log.txt
	-rpm -q --info -p $<

rule/profile%_x86_64:
	${make} ${gbs}/build profile=${@F} arch=x86_64

rule/profile%_i586:
	${make} ${gbs}/build profile=${@F} arch=i586

rule/profile%_armv6l:
	${make} ${gbs}/build profile=${@F} arch=armv6l

rule/profile%_armv7hl:
	${make} ${gbs}/build profile=${@F} arch=armv7hl

rule/profile%_armv7l:
	${make} ${gbs}/build profile=${@F} arch=armv7l

rule/profile%_aarch64:
	${make} ${gbs}/build profile=${@F} arch=aarch64

rule/profile%_mips:
	${make} ${gbs}/build profile=${@F} arch=mips

rule/profile%_mipsel:
	${make} ${gbs}/build profile=${@F} arch=mipsel

force/%:
	-mv -f ${tmpdir}/${@F} ${tmpdir}/${@F}.bak
	${make} profile=${@F} revision rule/${@F} revision-clean

check/%:
	-@ls "${tmpdir}/${gbs_builder}-${@F}.log._success.tmp" \
 || ${make} "force/${@F}" || echo "fail: ${@F}"
	-cat "${tmpdir}/${gbs_builder}-${@F}"
	-@ls "${tmpdir}/${gbs_builder}-${@F}.log._success.tmp" \

rule/check: ${gbs_file}
	${make} check/urls
	for profile in ${profiles} ; do \
 ${make} check/$${profile} \
 || echo "fail: $${profile}"\
	;  done
	@echo "# $@: Failures ? in ${tmpdir}"
	@ls ${tmpdir}/*.log._fail.tmp 2> /dev/null \
 && exit 1 || echo "# $@: Failures: NONE"


check:
	HOME=${CURDIR}/local/tmp/home/${USER} ${MAKE} rule/check

reset: ${gbs_file}
	${make} check/urls
	for profile in ${profiles} ; do \
 ${make} reset/$${profile} \
 || echo "fail: $${profile}"\
	;  done


check/urls: ${tmpdir}/urls-check
	@echo "# Failures ?"
	@[ -r $<._fail.tmp ] && cat $<._fail.tmp 2>&1 && exit 1 || echo "# NONE: $@"

${tmpdir}/urls-check: ${gbs_file}
	@mkdir -p ${@D}
	@grep 'url=' ${gbs_file} \
 | grep -v '#' \
 | sed -e 's|url=\(.*\)|\1|g' \
 | while read url ; do \
 wget -O /dev/null $${url}  >/dev/null 2>&1 \
 || echo "error: $@: $${url}" >> ${@}._fail.tmp ; \
 done
	@touch $@

#TODO: only change status?


umount:
	[ ! -e ${mount_dir} ] || ${sudo} umount ${mount_dir}


binfmt: /proc/sys/fs/binfmt_misc/
	@echo "TODO: add tests"
	ls "$<" | while read file ; do \
  echo '-1'  | ${sudo} tee "$</$${file}" ; \
	done

${gbs_file}: ${thisdir}/config/gbs/ ${srcs}
	find $< -iname "*.conf.in" -print \
 | sort | while read t ; do cat $${t} ; done | tee $@.tmp
	mv $@.tmp $@

osc-touch/%:
	mkdir -p "tmp/url/${osc_url}/${@F}/"
	osc -v -A${osc_url} meta prjconf ${@F} | tee "tmp/url/${osc_url}/${@F}/prjconf"
	osc -v -A${osc_url} repositories ${@F} | tee "tmp/url/${osc_url}/${@F}/repositories"

osc-ls:
	mkdir -p "tmp/url/${osc_url}/"
	osc -v -A${osc_url} ls | grep -i '^${project}:' \
 | while read project ; do ${make} osc-touch/$${project} ; done

all: ${gbs_conf}

commit:
	-rm -fv ${gbs_conf}
	${make} all
	-git commit -sm 'gbs: Generate config/gbs.conf' ${gbs_conf}

cleanall:
	-find ${tmpdir}/../tmp/ -exec rm -fv {} \;

/var/tmp/${user}-gbs/${profile}.conf:
	@echo "TODO: osc"
	exit 1

# compatible macro
profile.%: rule/profile.%
	@echo "done: $@: $^"


.PHONY: default help
